﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Threading;
using ScrumTable.BL.DM.DataManagement;
using Microsoft.Practices.Unity;
using ScrumTable.UI.ViewModel.Data;

#endregion

namespace ScrumTable.UI.ViewModel.Tools
{
    /// <summary>
    /// View Model for a simple menu
    /// </summary>
    public class ViewModelTeamSelector : ViewModelBase
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private ScrumViewModelData _scrumData;
        private MemberViewModel _member;
        #endregion


        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------


        /// <summary>
        /// Gets or sets the ScrumViewModelData
        /// </summary>
        /// <value>ScrumViewModelData</value>
        [Dependency]
        public ScrumViewModelData ScrumData { get { return _scrumData; } set { _scrumData = value; _scrumData.PropertyChanged += OnScrumDataChanged; } }

        

        public MemberViewModel Member
        {
            get
            {
                return _member ?? ScrumData.CurrentMember;
            }
            set
            {
                if (value == null || !value.CanLogIn)
                {
                    DispatcherTimer timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(100)};
                    timer.Tick += ((tSender, args) =>
                                       {
                                           //NOTE should not be here...local driver need this
                                           if (ScrumData.CurrentMember.CanLogIn)
                                           {
                                               Member = ScrumData.CurrentMember;
                                           }
                                           else
                                           {
                                               _member = null;
                                               SendPropertyChanged("Member");
                                           }
                                           var theTimer = tSender as DispatcherTimer;
                                           if (theTimer != null) theTimer.Stop();
                                       });
                    timer.Start();
                    _member = value;
                    SendPropertyChanged("Member");
                }
                else
                {
                    ScrumData.CurrentMember = value;
                }
                
            }
        }
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            _scrumData.PropertyChanged -= OnScrumDataChanged;
            base.ReleaseManagedResources();
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
     
        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        void OnScrumDataChanged(object sender, PropertyChangedEventArgs e)
        {
           if( e.PropertyName == "CurrentMember")
           {
               _member = ScrumData.CurrentMember;
               SendPropertyChanged("Member");
           }
        }
        #endregion

    }
}